﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace MR.Common
{
    public class Mission
    {
        [JsonProperty("missionId")]
        public Guid MissionId { get; set; }
        [JsonProperty("tid")]
        public string TID { get; set; }
        [JsonProperty("sent")]
        public string Sent { get; set; }
        [JsonProperty("targets")]
        public IList<Target> Targets { get; set; }

        public static Mission Establish(string sent) => new Mission()
        {
            MissionId = Guid.NewGuid(),
            Sent = sent,
            Targets = new List<Target>(),
        };
        public string ToCommand()
        {
            if (Targets.Count <= 0)
            {
                throw new Exception();
            }

            var missionHeader = "";
            var missionBody = "";
            Targets.ToList().ForEach(o =>
            {
                switch ((TargetAction)o.Action)
                {
                    case TargetAction.Pickup:
                        missionHeader += $"P{o.Src.Position}";
                        missionBody += $"SRC{o.Src.Position}={o.Src.Goal} LOT{o.Src.Position}={o.Src.Lot} QTY{o.Src.Position}={o.Src.Quantity} TYPE{o.Src.Position}={o.Src.Type} ";
                        break;
                    case TargetAction.Dropoff:
                        missionHeader += $"D{o.Dest.Position}";
                        missionBody += $"LOT{o.Dest.Position}={o.Dest.Lot} QTY{o.Dest.Position}={o.Dest.Quantity} TYPE{o.Dest.Position}={o.Dest.Type} DES{o.Dest.Position}={o.Dest.Goal} ";
                        break;
                    case TargetAction.Swap:
                        missionHeader += $"S{o.Dest.Position}{o.Src.Position}";
                        missionBody += $"DES{o.Dest.Position}={o.Dest.Goal} TYPE{o.Dest.Position}={o.Dest.Type} LOT{o.Dest.Position}={o.Dest.Lot} QTY{o.Src.Position}={o.Src.Quantity} TYPE{o.Dest.Position}={o.Dest.Type} SRC{o.Src.Position}={o.Src.Goal} ";
                        break;
                    default:
                        break;
                }
            });
            missionHeader += $" MISSIONID={Guid.NewGuid()} TID=1";
            // var missionHeader = "";
            // var pickupTargets = Targets.Where(o => o.Action == "P").OrderBy(o => o.Priority).ToList();
            // var dropoffTargets = Targets.Where(o => o.Action == "D").OrderByDescending(o => o.Priority).ToList();
            // var headerArr = new List<string>();
            // var placements = new List<ComplexTarget>();
            // pickupTargets.ForEach(o =>
            // {
            //     if(placements.Count() == 0)
            //     {
            //         var dropoffTarget = dropoffTargets.FirstOrDefault(t => t.Lot == o.Lot);
            //         var placement = new ComplexTarget();
            //         placement.Placed = true;
            //         placement.PickupId = o.Id;
            //         placement.DropoffId = dropoffTarget.Id;
            //         placements.Add(placement);
            //     }
            //     else
            //     {
            //         var dropoffTarget = dropoffTargets.FirstOrDefault(t => t.Goal == o.Goal);
            //         var placement = new ComplexTarget();
            //         placement.Placed = true;
            //         placement.PickupId = o.Id;
            //         placement.DropoffId = dropoffTarget.Id;

            //         if (dropoffTarget != null)
            //         {
            //             placement.Action = "S";

            //         }
            //         placements.Add(placement);
            //     }
            // });

            // var orderedTargets = Targets.OrderByDescending(o => o.Priority).OrderByDescending(o => o.Action == "P").ToList();

            // var grouped = orderedTargets.GroupBy(o => new { Lot = o.Lot, Goal = o.Goal}).ToList();
            // var groupTasks = new Dictionary<string,IList<Target>>();
            // orderedTargets.ForEach(o =>
            // {
            //     if (!groupTasks.Keys.Contains(o.Goal))
            //     {
            //         groupTasks.Add(o.Goal, new List<Target>());
            //     }
            //     var targets = groupTasks[o.Goal];

            //     if(targets.Count(t => t.Lot == o.Lot) == 0)
            //     {
            //         targets.Add(o);
            //     }
            //     missionHeader += (o.Action + o.Position);
            // });
            return missionHeader + " " + missionBody;
        }
    }
}
